﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BLL;
using BLL.Access;
using System.Data;
using Platform.Http;
using Web.Class;
using DevExpress.Web;
using System.Text.RegularExpressions;

namespace Web.Pages.QuShui.DiXiaS._1.JiChuXX
{
    public partial class ZhuGuanBMXX : BasePage
    {
        #region 属性

        /// <summary>
        ///当前操作项ID
        /// </summary>
        private int GongSiXXID
        {
            get
            {
                return Convert.ToInt32(ViewState["_GongSiXXID"]);
            }
            set
            {
                ViewState["_GongSiXXID"] = value;
            }
        }
        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        #endregion

        #region 变量


        QS_JX_BUMENXXBLL bumenBLL = new QS_JX_BUMENXXBLL();
        /// <summary>
        /// 公司信息BLL
        /// </summary>
        YS_JX_GONGSIXXBLL _YS_JX_GONGSIXXBLL = new YS_JX_GONGSIXXBLL();

        /// <summary>
        /// 操作日志BLL
        /// </summary>
        SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitControl();
                InitData();
               
            }
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
           
        }

        /// <summary>
        /// 绑定空表头
        /// </summary>
        protected override void Render(HtmlTextWriter sender)
        {

            base.Render(sender);
        }

        /// <summary>
        /// 响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, DevExpress.Web.CallbackEventArgs e)
        {
            int id = Convert.ToInt32(this.hdCollection.Get("ID"));
            
            if (e.Parameter == "save")
            {
                
                QS_JX_BUMENXX qs = new QS_JX_BUMENXX();
                OperateState operateState;
                //添加
                if (id < 0)
                {
                    string msg = "";
                    //if (!CheckSaveData(0,out msg))
                    //{
                    //    e.Result = e.Parameter + "|-1|"+msg;
                    //    return;
                    //}
                    //qs.S_BUMEN_GUID = Guid.NewGuid().ToString();
                    DataTable dt = bumenBLL.SelectQS_JX_BUMENXXesDynamic(" S_BUMENBH='" + txtBianHao.Text.Trim() + "' and i_jlzt=0", "", null);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        msg = "该编号已存在！";
                        e.Result = e.Parameter + "|-1|" + msg;
                        return;
                    }
                    operateState = OperateState.Add;
                    qs.ID = 0;
                    qs.D_TIANJIARQ = DateTime.Now;
                    qs.S_TIANJIAR = LoginInfo.CurrentUser.UserID.ToString();
                }
                else
                {
                    //string msg = "";
                    //if (!CheckSaveData(1, out msg))
                    //{
                    //    e.Result = e.Parameter + "|-1|" + msg;
                    //    return;
                    //}
                    //修改操作
                    operateState = OperateState.Modify;
                    qs = bumenBLL.SelectQS_JX_BUMENXX(id);
                }
                //从页面获取属性值
                qs.S_BUMENBH = this.txtBianHao.Text;
                qs.S_BUMENMC = this.txtMingCheng.Text;
                qs.S_DIZHI = this.txtDiZ.Text;
                qs.S_YOUBIAN = this.txtYouBian.Text;
                qs.S_LIANXIDH = this.txtLianXiDH.Text;
                qs.S_LIANXIBM = this.txtLianXiBM.Text;
                qs.S_LIANXIR = this.txtLianXiR.Text;
                qs.S_BEIZHU = this.txtBeiZhu.Text;
                qs.S_YEWUY_GUID = cmYeWuY.Value.ToString();
                qs.I_TUOSHOU = chkTuoSHou.Checked ? 1 : 0;
                qs.S_KAIHUHM = txtKaiHuHM.Text;
                qs.S_KAIHUYH = txtKaiHuYH.Text;
                qs.S_KAIHUZH = txtKaiHuZH.Text;
                qs.I_JLZT = 0;
                qs.D_CAOZUORQ = DateTime.Now;
                qs.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                LoadData(true);
                _SYS_LOGBLL.Log<QS_JX_BUMENXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "主管部门信息", operateState, qs, null);
                bumenBLL.InsertOrUpdate(qs);
                

                e.Result = e.Parameter + "|0|保存成功！|" + gvList.VisibleRowCount;
                

            }
            else if (e.Parameter == "deleteValidate")
            {
                //获取当前操作项
                QS_JX_BUMENXX qs = bumenBLL.SelectQS_JX_BUMENXX(id);
                //确认是否有用户使用
                if (new YS_YG_YONGHUXXBLL().SelectYS_YG_YONGHUXXesDynamic(string.Format("s_gongsibh='{0}'", qs.S_BUMENBH), null).Rows.Count > 0)
                {
                    e.Result = e.Parameter + "|-1|当前主管部门系统正在使用,不允许删除。";
                }
                else
                {
                    e.Result = e.Parameter + "|0|success";
                }
            }
            else if (e.Parameter == "delete")
            {
                QS_JX_BUMENXX qs = bumenBLL.SelectQS_JX_BUMENXX(id);
                qs.D_CAOZUORQ = DateTime.Now;
                qs.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                qs.I_JLZT = -1;
                bumenBLL.InsertOrUpdate(qs);
                _SYS_LOGBLL.Log<QS_JX_BUMENXX>(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "主管部门信息", OperateState.Delete, qs, null);
                e.Result = e.Parameter + "|0|删除成功！";
               
            }           
            else if (e.Parameter == "setinfo")
            {
                string msg = "";
                QS_JX_BUMENXX qs = bumenBLL.SelectQS_JX_BUMENXX(id);
                if (qs != null)
                {
                    msg = string.Join("|", qs.S_BUMENBH,qs.S_BUMENMC,qs.S_YOUBIAN,qs.S_DIZHI,qs.S_LIANXIR,qs.S_LIANXIBM,qs.S_LIANXIDH,qs.S_YEWUY_GUID,qs.S_KAIHUHM,qs.S_KAIHUYH,
                        qs.S_KAIHUZH,qs.S_BEIZHU,qs.I_TUOSHOU);
              
                } 
                e.Result = e.Parameter + "|0|" + msg;
            }
            
            
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        protected void gvList_CustomCallback(object sender, DevExpress.Web.ASPxGridViewCustomCallbackEventArgs e)
        {
            LoadData(true);
            
        }

        #endregion

        #region 方法

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitControl()
        {
            LoadData(false);
            
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData()
        {
            this.txtBeiZhu.Text = "";
            this.txtBianHao.Text = "";
            this.txtDiZ.Text = "";
            this.txtLianXiBM.Text = "";
            this.txtLianXiDH.Text = "";
            this.txtLianXiR.Text = "";
            this.txtMingCheng.Text = "";
            this.txtYouBian.Text = "";
            PageTools.QSLoadCombox(cmYeWuY, PageTools.eLoadType.业务员QS, null, "");
        }
        //private bool CheckSaveData(int type,out string msg)
        //{
        //    bool isSuccess = true;
        //    msg = "操作成功";
        //    if (txtBianHao.Text.Trim().Length == 0)
        //    {
        //        msg = "编号不能为空";
        //        isSuccess = false;
        //    }
        //    //添加
        //    if (type == 0)
        //    {
        //        DataTable dt = bumenBLL.SelectQS_JX_BUMENXXesDynamic(" S_BUMENBH='" + txtBianHao.Text.Trim() + "' and i_jlzt=0", "", null);
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            msg = "该编号已存在！";
        //            isSuccess = false;
        //        }
        //    }
        //    else//修改
        //    {
        //        int id = Convert.ToInt32(this.hdCollection.Get("ID"));
        //        DataTable dt = bumenBLL.SelectQS_JX_BUMENXXesDynamic(" S_BUMENBH='" + txtBianHao.Text.Trim() + "' and i_jlzt=0 and id<>" + id, "", null);
        //        if (dt != null && dt.Rows.Count > 0)
        //        {
        //            msg = "该编号已存在！";
        //            isSuccess = false;
        //        }
        //    }
        //    if (txtMingCheng.Text.Trim().Length == 0)
        //    {
        //        msg = "名称不能为空";
        //        isSuccess = false;
        //    }
        //    if (txtYouBian.Text.Trim().Length == 0)
        //    {
        //        msg = "邮编不能为空";
        //        isSuccess = false;
        //    }
        //    //,, ,,ckTuoSHou,,,
        //    if (txtDiZ.Text.Trim().Length == 0)
        //    {
        //        msg = "地址不能为空";
        //        isSuccess = false;
        //    }
        //    if (txtLianXiR.Text.Trim().Length == 0)
        //    {
        //        msg = "联系人不能为空";
        //        isSuccess = false;
        //    }
        //    if (txtLianXiBM.Text.Trim().Length == 0)
        //    {
        //        msg = "联系部门不能为空";
        //        isSuccess = false;
        //    }
        //    if (txtLianXiDH.Text.Trim().Length == 0)
        //    {
        //        msg = "联系电话不能为空";
        //        isSuccess = false;
        //    }
        //    if (cmYeWuY.Text.Trim().Length == 0)
        //    {
        //        msg = "业务员不能为空";
        //        isSuccess = false;
        //    }
        //    if (ckTuoSHou.Checked)
        //    {
        //        if (txtKaiHuHM.Text.Trim().Length == 0)
        //        {
        //            msg = "开户户名不能为空";
        //            isSuccess = false;
        //        }
        //        if (txtKaiHuYH.Text.Trim().Length == 0)
        //        {
        //            msg = "开户银行不能为空";
        //            isSuccess = false;
        //        }
        //        if (txtKaiHuZH.Text.Trim().Length == 0)
        //        {
        //            msg = "开户账号不能为空";
        //            isSuccess = false;
        //        }
        //    }
           
        //    return isSuccess;
           

        //}
        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData(bool reget)
        {
            int wtype = BusinessTool.WType;
            this.dtSource = bumenBLL.SelectQS_JX_BUMENXXesDynamic(string.Format(" i_jlzt=0 and I_WATERTYPE={0}", wtype), null);             
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
        }

        
        #endregion
    }
}